package com.soft.mainsite.module.web.cms;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.web.cms.service.SilderService;
import com.soft.mainsite.module.web.system.vo.UserVo;
import com.soft.mainsite.mybatis.model.TCmsAdSilder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description:幻灯片广告
 *
 * @author Jin
 * @create 2017-06-12
 **/
@Controller
@RequestMapping("/system/cms/ad/silder")
public class SilderController {

    @Autowired
    private SilderService silderService;

    @RequiresPermissions("indexsilder:admin")
    @RequestMapping
    public String index(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
                        TCmsAdSilder pojo, Model model, HttpServletRequest request) {
        // 获取会话用户
        UserVo userVo = (UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
        pojo.setSiteId(userVo.getSiteId());

        PageInfo page = silderService.page(pageNumber, pageSize, pojo);
        model.addAttribute("model", page);
        model.addAttribute("pojo", pojo);
        return "cms/ad_silder";
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(TCmsAdSilder pojo, HttpServletRequest request) {
        // 获取会话用户
        UserVo userVo = (UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
        if (StrUtil.isBlank(pojo.getImg())) {
            pojo.setImg(null);
        }
        if (pojo.getId() != null) {
            return silderService.update(pojo, userVo);
        }
        return silderService.save(pojo, userVo);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam(value = "ids", required = false) Integer[] ids, HttpServletRequest request) {
        // 获取会话用户
        UserVo userVo = (UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
        return silderService.delete(ids, userVo);
    }

    @RequestMapping("/findListBySite")
    @ResponseBody
    public List<TCmsAdSilder> findListBySite(HttpServletRequest request) {
        TCmsAdSilder pojo = new TCmsAdSilder();
        // 获取会话用户
        UserVo userVo = (UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
        pojo.setSiteId(userVo.getSiteId());
        List<TCmsAdSilder> list = silderService.findList(pojo);
        return list;
    }
}
